﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JXT.PrimaryKey.Batman.Collections
{
    /// <summary>
    /// 分页集合
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Serializable]
    public class PagedCollection<T> : List<T>, IPagedCollection<T>
    {
        /// <summary>
        /// 记录数
        /// </summary>
        public int RecordCount { get; private set; }

        /// <summary>
        /// 分页大小
        /// </summary>
        public int PageSize { get; private set; }

        /// <summary>
        ///  当前页
        /// </summary>
        public int PageIndex { get; private set; }

        /// <summary>
        /// 页码统计
        /// </summary>
        public int PageCount
        {
            get
            {
                return RecordCount % PageSize == 0 ?
                    RecordCount / PageSize :
                    RecordCount / PageSize + 1;
            }
        }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="recordCount">记录数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">分页大小</param>
        public PagedCollection(int recordCount, int pageIndex, int pageSize)
        {
            RecordCount = recordCount;
            if (pageIndex <= 0)
                pageIndex = 1;
            if (pageSize <= 0)
                pageSize = 20;
            PageSize = pageSize;
            PageIndex = pageIndex;
        }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="recordCount">记录数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="enumerable">集合</param>
        public PagedCollection(int recordCount, int pageIndex, int pageSize, IEnumerable<T> enumerable)
            : this(recordCount, pageIndex, pageSize)
        {
            AddRange(enumerable);
        }
    }
}
